package com.mosai.alignpro;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewStub;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mosai.alignpro.entity.ImageInfo;
import com.mosai.alignpro.util.DialogUtils;
import com.mosai.alignpro.util.ToastUtils;

public class EditDescribeActivity extends BaseActivity {

    public static final String EXTRA_DESCRIBE = "EXTRA_DESCRIBE";
    public static final String EXTRA_SCOLIOSIS_TYPE = "EXTRA_SCOLIOSIS_TYPE";
    public static final String EXTRA_SHOW_SCOLIOSIS_TYPE = "EXTRA_SHOW_SCOLIOSIS_TYPE";
    public static final String EXTRA_CONDITION_STATUS = "EXTRA_CONDITION_STATUS";
    public static final String EXTRA_EDITABLE = "EXTRA_EDITABLE";
    public static final int REQUEST_CODE = 10006;

    private EditText etContent;
    private RadioGroup rgType, rgSeverity;
    private boolean editable;

    public static void startForResult(Activity activity, ImageInfo item) {
        if (item.getImageType() == ImageInfo.IMAGE_TYPE_BACK) {
            EditDescribeActivity.startForResult(activity, item.getNewDescribe(), item.getNewScoliosisType(), item.getNewConditionStatus(), item.isEditing(), true);
        } else {
            EditDescribeActivity.startForResult(activity, item.getNewDescribe(), 0, 0, item.isEditing(), false);
        }
    }

    public static void startForResult(Activity activity, String describe, int scoliosisType, int conditionStatus, boolean editable, boolean showScoliosisType) {
        if (activity == null) return;
        Intent intent = new Intent(activity, EditDescribeActivity.class);
        intent.putExtra(EXTRA_DESCRIBE, describe);
        intent.putExtra(EXTRA_SCOLIOSIS_TYPE, scoliosisType);
        intent.putExtra(EXTRA_SHOW_SCOLIOSIS_TYPE, showScoliosisType);
        intent.putExtra(EXTRA_CONDITION_STATUS, conditionStatus);
        intent.putExtra(EXTRA_EDITABLE, editable);
        activity.startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_describe);
        initView();
        initData();
    }

    private void initView() {
        etContent = findViewById(R.id.etContent);
    }

    private void initData() {
        editable = getIntent().getBooleanExtra(EXTRA_EDITABLE, true);
        etContent.setText(getIntent().getStringExtra(EXTRA_DESCRIBE));
        etContent.setEnabled(editable);
        if (!editable) {
            findViewById(R.id.btnSave).setVisibility(View.GONE);
        }

        boolean showScoliosisType = getIntent().getBooleanExtra(EXTRA_SHOW_SCOLIOSIS_TYPE, false);
        if (!showScoliosisType) return;
        int scoliosisType = getIntent().getIntExtra(EXTRA_SCOLIOSIS_TYPE, 0);
        int conditionStatus = getIntent().getIntExtra(EXTRA_CONDITION_STATUS, 0);
        ViewStub layoutTypeWithSeverity = findViewById(R.id.layoutTypeWithSeverity);
        View view = layoutTypeWithSeverity.inflate();
        rgType = view.findViewById(R.id.rgType);
        setCheckedByTag(rgType, scoliosisType);
        rgSeverity = view.findViewById(R.id.rgSeverity);
        setCheckedByTag(rgSeverity, conditionStatus);
    }

    private void setCheckedByTag(RadioGroup radioGroup, Object value) {
        String tag = value.toString();
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
            if (tag.equals(radioButton.getTag())) {
                radioButton.setChecked(true);
                break;
            }
        }
        if (editable) return;
        for (int i = 0; i < radioGroup.getChildCount(); ) {
            RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
            if (!radioButton.isChecked()) {
                radioGroup.removeViewAt(i);
                continue;
            }
            RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
            lp.topMargin = 0;
            lp.leftMargin = 0;
            lp.rightMargin = 0;
            lp.bottomMargin = 0;
            lp.weight = 0;
            radioButton.setLayoutParams(lp);
            i++;
        }
    }

    public void onClickBack(View view) {
        if (!editable) {
            finish();
            return;
        }
        DialogUtils.showCancelEditDialog(this, new DialogUtils.OnClickListener() {
            @Override
            protected void onClick(DialogInterface dialog, boolean isPositive) {
                dialog.dismiss();
                if (isPositive) {
                    finish();
                }
            }
        });
    }

    public void onClickSave(View view) {
        Editable text = etContent.getText();
        if (TextUtils.isEmpty(text)) {
            ToastUtils.getInstance().show(R.string.msg_describe_empty);
            return;
        }
        Intent data = new Intent();
        data.putExtra(EXTRA_DESCRIBE, text.toString());
        if (rgType != null) {
            try {
                String scoliosisType = (String) rgType.findViewById(rgType.getCheckedRadioButtonId()).getTag();
                data.putExtra(EXTRA_SCOLIOSIS_TYPE, Integer.parseInt(scoliosisType));
                String conditionStatus = (String) rgSeverity.findViewById(rgSeverity.getCheckedRadioButtonId()).getTag();
                data.putExtra(EXTRA_CONDITION_STATUS, Integer.parseInt(conditionStatus));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        setResult(RESULT_OK, data);
        finish();
    }

    @Override
    public void onBackPressed() {
        onClickBack(null);
    }
}
